package org.logtime.blog.module;

import java.io.Serializable;
import java.util.List;

/**
 * @version: 1.0
 * @Author: itmins
 * @Email: itmins@gmail.com
 */
@SuppressWarnings("serial")
public class PageModule<T> implements Serializable {

	/**
	 * Search begin
	 */
	private int offset;
	/**
	 * Now page
	 */
	private int nowPage;
	/**
	 * Pre page
	 */
	private int prePage;
	/**
	 * Next page
	 */
	private int nextPage;
	/**
	 * Every page size
	 */
	private int pageSize;
	/**
	 * Model list
	 */
	private List<T> data;
	/**
	 * Total page
	 */
	private int totalPage;
	/**
	 * Now data number
	 */
	private int nowDataNum;
	/**
	 * Total data number
	 */
	private int totalDataNum;
	/**
	 * Model class
	 */
	private Class<T> modelClass;

	/**
	 * Defalult int
	 */
	public static final int DEFAULT_PAGE_SIZE = 1000;

	public PageModule(Class<T> c, List<T> data) {
		this(c, 1, DEFAULT_PAGE_SIZE);
		if (null != data) {
			this.init(data.size(), data);
		}
	}

	public PageModule(Class<T> c, int page, int pageSize) {
		if (page > pageSize) {
			page = pageSize;
		}
		this.modelClass = c;
		this.pageSize = pageSize <= 0 ? 1 : pageSize;
		this.nowPage = (page < 1) ? 1 : page;
		this.offset = pageSize * (this.nowPage - 1);
	}

	public PageModule<T> init(int totalDataNum, List<T> data) {
		this.data = data;
		this.nowDataNum = (null == data) ? 0 : data.size();
		this.totalDataNum = totalDataNum;
		this.totalPage = ((totalDataNum - 1) / pageSize) + 1;
		this.nextPage = (this.nowPage + 1 > totalPage) ? totalPage
				: this.nowPage + 1;
		this.prePage = (this.nowPage < 2) ? 1 : (this.nowPage - 1);
		return this;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getNowDataNum() {
		return nowDataNum;
	}

	public void setNowDataNum(int nowDataNum) {
		this.nowDataNum = nowDataNum;
	}

	public int getTotalDataNum() {
		return totalDataNum;
	}

	public void setTotalDataNum(int totalDataNum) {
		this.totalDataNum = totalDataNum;
	}

	public Class<T> getModelClass() {
		return modelClass;
	}

	public void setModelClass(Class<T> modelClass) {
		this.modelClass = modelClass;
	}
}
